package com.apop.mess;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RunWith(SpringRunner.class)
@Slf4j
public class UnitTest1 {

    @Test
    public void generateTokenTest(){
        log.info("生成token测试");
        String token = UUID.randomUUID().toString().substring(0,16);
        log.info("token {}",token);
    }

    @Test
    public void paperTest(){
       System.out.println("最多折 "+foldPaper(0.01,29.7,21,1)+" 次");
    }

    /**
     * 折纸
     * @param thickness 厚度 mm
     * @param long_ 长度 mm
     * @return
     */
    private int foldPaper(double thickness,double long_,double width,int time){
        double a = long_;
        double b = width;
        if(b > a){// 如果宽大于长，交换
            double temp = long_;
            a = width;
            b = long_;
        }
        if(Math.max(a,b) > thickness){
            System.out.println("第 "+time+" 次对折  厚度变成 "+thickness*2+"mm 长度变成 "+a/2+" mm"+" 宽度变成 "+b/2+" mm");
            return 1+foldPaper(thickness*2,a/2,b,time+1);
        }
        return 0;
    }

    @Test
    public void regTest() {
        // 按指定模式在字符串查找
        String INPUT = "[\"350100\"]";

        String REGEX = "\\d+";

        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(INPUT); // 获取 matcher 对象
        if(m.find()){
            System.out.println("group "+m.group(0));
        }
    }

    @Test
    public void fileTest(){
        try {
            final String path = "actions";
            ClassPathResource classPathResource = new ClassPathResource(path);
            // 获得File对象，当然也可以获取输入流对象
            File file = classPathResource.getFile();
            // 列出下面所有
            File[] fileList = file.listFiles();
            log.info("actions目录下文件==========");
            for(File f : fileList){
                log.info(f.getName());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
